package com.jhx.video.store

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.jhx.common.activity.PicturePreviewActivity
import com.jhx.common.framework.QuickBaseFragment
import com.jhx.common.model.Photos
import com.jhx.common.utils.DisplayUtil
import com.jhx.common.utils.ImageLoader
import com.jhx.video.R
import kotlinx.android.synthetic.main.fragment_photo.*

/**
 * 照片管理
 *
 * @author Jack 2018-03-27 10:59
 */
class PhotoFragment : QuickBaseFragment(), Contract.PhotoView {
    private var mPhotoList: ArrayList<String> = ArrayList()
    private var mPresenter: PhotoPresenterImpl? = null
    private var mAdapter: ViewAdapter? = null
    private var mEmptyView: View? = null
    private var mPageSize  = 20
    private var mPageIndex = 1

    override fun bindView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_photo, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mEmptyView = view?.findViewById(R.id.empty_layout)
    }

    @SuppressLint("MissingSuperCall")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mAdapter = ViewAdapter()
        mPresenter = PhotoPresenterImpl()
        mPresenter?.setView(this)
        viewpager.adapter = mAdapter
        mPresenter?.getPhoto(mPageIndex, mPageSize)
        viewpager.addOnPageChangeListener(object: ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            @SuppressLint("SetTextI18n")
            override fun onPageSelected(position: Int) {
                txt_number.text = "${position + 1}/${mPhotoList.size}"
            }

        })
    }

    inner class ViewAdapter : PagerAdapter() {
        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun getCount(): Int {
            return mPhotoList.size
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = ImageView(activity)
            view.setOnClickListener {
                val intent = Intent(activity, PicturePreviewActivity::class.java)
                val urlList = ArrayList<String>()
                mPhotoList.forEach {
                    urlList.add(it)
                }
                intent.putExtra(PicturePreviewActivity.KEY_DATA, urlList)
                intent.putExtra(PicturePreviewActivity.KEY_INDEX, position)
                startActivity(intent)
            }
            val params = ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.MATCH_PARENT, DisplayUtil.dip2px(activity, 200f))
            view.layoutParams = params
            ImageLoader.get().loadImageByUrl(activity, mPhotoList[position], view)
            container.addView(view)
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeViewAt(position)
        }

        override fun getItemPosition(`object`: Any): Int {
            return POSITION_NONE
        }
    }

    override fun showLoading() {
        showLoadingDialog()
    }

    override fun hideLoading() {
        hideLoadingDialog()
    }

    @SuppressLint("SetTextI18n")
    override fun getPhotoSuccess(photos: Photos?) {
        if(photos != null && !TextUtils.isEmpty(photos.imgUrl)) {
            val photosList = photos.imgUrl.split(",".toRegex()).dropLastWhile({ it.isEmpty() }).toList()
            if(photosList.isNotEmpty()) {
                mPhotoList.addAll(photosList)
                txt_number.text = "1/${mPhotoList.size}"
            }
            mAdapter?.notifyDataSetChanged()
            mEmptyView?.visibility = View.GONE
        } else {
            txt_number.visibility = View.GONE
            mEmptyView?.visibility = View.VISIBLE
        }
    }

    override fun getContext(): Activity? {
        return activity
    }
}